package com.ruoyi.cms.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cms.domain.vo.CmsArticleCateVo;
import com.ruoyi.cms.domain.bo.CmsArticleCateBo;
import com.ruoyi.cms.service.ICmsArticleCateService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章分类
 *
 * @author guorui
 * @date 2023-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/articleCate")
public class CmsArticleCateController extends BaseController {

    private final ICmsArticleCateService iCmsArticleCateService;

    /**
     * 查询文章分类列表
     */
    @SaCheckPermission("cms:articleCate:list")
    @GetMapping("/list")
    public TableDataInfo<CmsArticleCateVo> list(CmsArticleCateBo bo, PageQuery pageQuery) {
        return iCmsArticleCateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章分类列表
     */
    @SaCheckPermission("cms:articleCate:export")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsArticleCateBo bo, HttpServletResponse response) {
        List<CmsArticleCateVo> list = iCmsArticleCateService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章分类", CmsArticleCateVo.class, response);
    }

    /**
     * 获取文章分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cms:articleCate:query")
    @GetMapping("/{id}")
    public R<CmsArticleCateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCmsArticleCateService.queryById(id));
    }

    /**
     * 新增文章分类
     */
    @SaCheckPermission("cms:articleCate:add")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmsArticleCateBo bo) {
        return toAjax(iCmsArticleCateService.insertByBo(bo));
    }

    /**
     * 修改文章分类
     */
    @SaCheckPermission("cms:articleCate:edit")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmsArticleCateBo bo) {
        return toAjax(iCmsArticleCateService.updateByBo(bo));
    }

    /**
     * 删除文章分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cms:articleCate:remove")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCmsArticleCateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
